package javabase.schoolwork.day6_19;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.net.Socket;

/**
 * @author fance
 * @date 2018/6/19 20:53
 */
public class ThreadServer extends  Thread {
    private Socket client;

    public void setClient(Socket client) {
        this.client = client;
    }

    @Override
    public void run() {
        PrintStream out = null;
        BufferedReader bufferedReader = null;
       try {
           out = new PrintStream(client.getOutputStream());
           bufferedReader =
                   new BufferedReader(new InputStreamReader(
                           client.getInputStream()
                   ));

           boolean tmp = true;
           while (tmp) {
               String s = bufferedReader.readLine();
               if ("bye".equals(s)) {
                   tmp = false;
               } else {
                   out.println("server>>: " + s);
               }
           }
       } catch (Exception e) {

       }
        out.close();
        try {
            bufferedReader.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        try {
            client.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
